<?php
require_once "./upload_file.php";
require_once "./config/share_config.php";
$configfile = './config/share_config.php';
$config = require_once './config/config.php';
$shareUrl = 'http://'.$_SERVER["SERVER_NAME"].dirname($_SERVER["PHP_SELF"]).'/share.php';
$outUrl = 'http://'.$_SERVER["SERVER_NAME"].dirname($_SERVER["PHP_SELF"]).'/index.php';
$upload_path = iconv("UTF-8", "GBK", "upload/images/");
$upload = new upload(); 
// var_dump($_FILES);die();
if ($_GET['act'] == 'upload') {
	$result = $upload->uploadImage($_FILES['file']);
	if ($result['code'] == 1) {
		// echo $result['msg'].'<br>'.$result['imgUrl'];
		echo  json_encode($result, true);
		die();
	}else{
		echo  json_encode($result, true);
		die();
	}
}
if ($_GET['act'] == 'getImgs') {
	$result = $upload->getDir($upload_path);
	foreach ($result as $key => $value) {
		$result[$key] = 'http://'.$_SERVER["SERVER_NAME"].dirname($_SERVER["PHP_SELF"]).'/'.$upload_path.$value;
	}
	echo  json_encode($result, true);
	die();
}

if ($_GET['act'] == 'share') {
	$id = $_POST['id'];
	foreach ($_POST as $key => $value) {
		if ($key !== 'id' ) {
			if (empty($value)) {
				$result = [
			    	'code' => 0,
			    	'msg' => $key . "不能为空！ "
			    ];		    
				echo  json_encode($result, true);
				die();
			}
		}		
	}
	$data = [
		'page' => $_POST['page'],
		'title' =>str_replace(PHP_EOL, '', $_POST['title']),
		'imgUrl' => $_POST['imgurl'],
		'desc' => str_replace(PHP_EOL, '', $_POST['desc']),
	];
	if (!empty($id)) {
		$shareData[$id] = $data;
	}else{
		if (is_array($shareData)) {
		array_push($shareData, $data);
		}else{
			$shareData = array($data);
		}
		end($shareData);
		$id = each($shareData)['key'];
	}	
	$shareData[$id]['link'] = $outUrl.'?id='.$id.'&page='.$data['page'];
	//写入配置文件中
	$text='<?php $shareData='.var_export($shareData,true).';';  
	if(false!==fopen($configfile,'w+')){  
	    file_put_contents($configfile,$text);
	    
	    $result = [
		    	'code' => 1,
		    	'msg' => '设置分享链接成功！',
		    	'url' => $shareUrl.'?id='.$id
		];
		echo json_encode($result);
		die();
	}else{
		$result = [
		    	'code' => 0,
		    	'msg' => '设置分享链接失败！'
		];
		echo json_encode($result);
	    die();
	}
	
}

if ($_GET['act'] == 'del') {
	$key = $_POST['id'];
	unset($shareData[$key]);
	$text='<?php $shareData='.var_export($shareData,true).';';  
	if(false!==fopen($configfile,'w+')){  
	    file_put_contents($configfile,$text);
	    
	    $result = [
		    	'code' => 1,
		    	'msg' => '删除链接成功！'
		];
		echo json_encode($result);
		die();
	}else{
		$result = [
		    	'code' => 0,
		    	'msg' => '删除链接失败'
		];
		echo json_encode($result);
	    die();
	}
}

if ($_GET['act'] == 'configset') {
	$data = [
		'linkUrl' => $_POST['linkUrl']
	];
	$linkUrls = json_decode($config['linkUrls']);
	if (in_array($data['linkUrl'],$linkUrls)) {
		if(setconfig($data)){
			$result = [
		    	'code' => 1,
		    	'msg' => '设置落地页成功！'
			];
			echo json_encode($result);
	    	die();
		}
	}
	$result = [
    	'code' => 0,
    	'msg' => '设置落地页失败！'
	];
	echo json_encode($result);
	die();
	

}


 /**
 * 修改config的函数
 * @param $data 传入数组，配置前缀和对应的值 $data = [ 'default_return_type' => 'json' ];
 * @return bool 返回状态
 */
function setconfig($data)
{
    /**
     * 原理就是 打开config配置文件 然后使用正则查找替换 然后在保存文件.
     * 传递的参数为2个数组 前面的为配置 后面的为数值.  正则的匹配为单引号  如果你的是分号 请自行修改为分号
     * $pat[0] = 参数前缀;  例:   default_return_type
       $rep[0] = 要替换的内容;    例:  json
       $data = [ 'default_return_type' => 'json' ];
     */
    $pats = array();
    $reps = array();
    if (is_array($data)) {
    	foreach ($data as $key => $value) {
    		$pats[] = '/\'' . $key . '\'(.*?),/';
    		$reps[] = "'". $key. "'". "=>" . "'".$value ."',";
    	}
    	$fileurl = "./config/config.php";
        $string = file_get_contents($fileurl); //加载配置文件
        $string = preg_replace($pats, $reps, $string); // 正则查找然后替换
        file_put_contents($fileurl, $string); // 写入配置文件
        return true;
    } else {
        return flase;
    }
}
